package com.example.clxy.service.impl;

import com.example.clxy.dao.AnnouncementMapper;
import com.example.clxy.entity.Announcement;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.AnnouncementResult;
import com.example.clxy.result.business.PageResult.AnnouncementPageResult;
import com.example.clxy.service.AnnouncementService;
import com.example.clxy.util.AnnouncementFileUtil;
import com.example.clxy.util.FileUtil;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Hsieh
 * @title: AnnouncementServicelmpl
 * @projectName clxy
 * @description: AnnouncementService接口实现类
 * @date 2020/8/414:04
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class AnnouncementServicelmpl implements AnnouncementService {

    @Autowired
    AnnouncementMapper announcementMapper;
    @Autowired
    FileServiceImpl fileServiceImp;

/**
* @Author:hsieh
* @Date: 2020/8/4 14:25
  * @param
* @param page
 * @param pagesize
 * @Description:获取所有通知
 *
 */

    @Override
    public PageResult<AnnouncementPageResult> getAnnouncementList(String title, int page, int pagesize) {
        PageResult<AnnouncementPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<AnnouncementPageResult> list = announcementMapper.getAnnouncementList(title, begin, end, pagesize);

        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取通知列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取通知列表失败！");
        }
        return result;
    }
    /**
    * @Author:hsieh
    * @Date: 2020/8/5 15:46
      * @param
     * @Description:获取所有动态
     *
     */
    @Override
    public PageResult<AnnouncementPageResult> getNewsList(String title, int page, int pagesize) {
        PageResult<AnnouncementPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<AnnouncementPageResult> list = announcementMapper.getNewsList(title, begin, end, pagesize);

        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取公告列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取公告列表失败！");
        }
        return result;
    }
    /**
    * @Author:hsieh
    * @Date: 2020/8/7 11:27
      * @param
     * @Description:根据id获取单个通知或动态
     *
     */
    @Override
    public AnnouncementResult getAnnouncementbyid(String announcementid) {
        AnnouncementResult announcementResult=new AnnouncementResult();
        AnnouncementResult result=announcementMapper.getAnnouncementbyid(announcementid);
        if (result!=null){
            announcementResult=result;
            announcementResult.setResult(WebRestResult.SUCCESS);
            announcementResult.setMessage("查询成功！");
        }else {
            announcementResult.setResult(WebRestResult.FAILURE);
            announcementResult.setMessage("查询失败！");
        }
        return announcementResult;
    }

    /**
    * @Author:hsieh
    * @Date: 2020/8/4 14:28
      * @param
     * @Description:添加通知
     *
     */
    @Override
    public WebRestResult addAnnouncement(Announcement announcement) {
        WebRestResult result=new WebRestResult();
        announcement.setAnnouncementid(UUIDUtil.randomUUID());
        announcement.setFlag("0");
        announcement.setCreatedTime(new Date());
        announcement.setUpdatedTime(new Date());
        if (announcementMapper.insert(announcement)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("添加成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("添加失败！");
        }
        return result;
    }
    /**
    * @Author:hsieh
    * @Date: 2020/8/4 15:28
      * @param
     * @Description:修改通知
     *
     */
    @Override
    public WebRestResult updateAnnouncement(Announcement announcement) {
        WebRestResult result = new WebRestResult();
        announcement.setUpdatedTime(new Date());
        if (announcementMapper.updateByPrimaryKey(announcement)==1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新失败！");
        }
        return result;
    }
    /**
    * @Author:hsieh
    * @Date: 2020/8/4 15:38
      * @param
     * @Description:删除通知
     *
     */
    @Override
    public WebRestResult deleteAnnouncement(String announcementid) {
        WebRestResult result = new WebRestResult();
        if (announcementMapper.deleteAnnouncement(announcementid)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败！");
        }
        return result;
    }

    @Override
    public WebRestResult uploadPhoto(MultipartFile file, String announcementid) {
        if(AnnouncementFileUtil.validateFile(file).getResult()==WebRestResult.FAILURE){
            return AnnouncementFileUtil.validateFile(file);
        }
        WebRestResult result = new WebRestResult();
        String filePath="announcement\\";
        String absolutePath = AnnouncementFileUtil.getAbsolutePath(filePath);
        //System.out.println(absolutePath);
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String dateTime=sdf.format(date);
        String filename=dateTime+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        WebRestResult upload=fileServiceImp.upload(file,absolutePath,filename);
        if (upload.getResult()==WebRestResult.FAILURE){
            return upload;
        }
        String newFileName=(filePath+filename).replace("\\","/");
        result.setResult(WebRestResult.SUCCESS);
        result.setMessage(AnnouncementFileUtil.FilePathUrl+newFileName);
        return result;
    }
}
